//
//  BTEBaseViewController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit


class BTEBaseViewController: UIViewController {
    
    private(set) var navigationBar: UIView!
    private(set) var navigationBarBackground: UIView!
    private(set) var titleLabel: UILabel!
    private(set) var backButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setupNavigationBar()
        updateBackButtonVisibility()
    }
    
    // MARK: - Navigation Bar Setup
    private func setupNavigationBar() {
        navigationBar = UIView()
        navigationBarBackground = UIView()
        
        navigationBar.backgroundColor = .white
        navigationBarBackground.backgroundColor = .white
        
        view.addSubview(navigationBarBackground)
        view.addSubview(navigationBar)
        
        navigationBarBackground.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            navigationBarBackground.topAnchor.constraint(equalTo: view.topAnchor),
            navigationBarBackground.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            navigationBarBackground.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            navigationBarBackground.heightAnchor.constraint(equalToConstant: navigationBarHeight)
        ])
        
        navigationBar.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            navigationBar.heightAnchor.constraint(equalToConstant: 44)
        ])
        
        // Title
        titleLabel = UILabel()
        titleLabel.font = .boldSystemFont(ofSize: 18)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .black
        navigationBar.addSubview(titleLabel)
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            titleLabel.centerXAnchor.constraint(equalTo: navigationBar.centerXAnchor),
            titleLabel.centerYAnchor.constraint(equalTo: navigationBar.centerYAnchor)
        ])
        
        // Back Button
        backButton = UIButton(type: .system)
        backButton.setImage(UIImage(named: "fewfewfwefwefew"), for: .normal)
        backButton.addTarget(self, action: #selector(didTapBackButton), for: .touchUpInside)
        
        backButton.layer.shadowColor = UIColor.black.cgColor
        backButton.layer.shadowOpacity = 0.06
        backButton.layer.shadowOffset = .zero
        
        navigationBar.addSubview(backButton)
        
        backButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            backButton.widthAnchor.constraint(equalToConstant: 32),
            backButton.heightAnchor.constraint(equalToConstant: 32),
            backButton.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor, constant: 16),
            backButton.centerYAnchor.constraint(equalTo: navigationBar.centerYAnchor)
        ])
    }
    
    // MARK: - Actions
    @objc private func didTapBackButton() {
        navigationController?.popViewController(animated: true)
    }
    
    // MARK: - Public API
    
    /// 更新导航栏标题
    func updateNavigationTitle(_ title: String) {
        titleLabel.text = title
    }
    
    /// 显示或隐藏自定义导航栏
    func setNavigationBarHidden(_ hidden: Bool, animated: Bool = false) {
        let changes = {
            self.navigationBar.alpha = hidden ? 0 : 1
            self.navigationBarBackground.isHidden = hidden
        }
        animated ? UIView.animate(withDuration: 0.25, animations: changes) : changes()
    }
    
    /// 设置导航栏背景透明
    func setNavigationBarTransparent() {
        navigationBar.backgroundColor = .clear
        navigationBarBackground.backgroundColor = .clear
    }
    
    /// 根据当前层级更新返回按钮状态
    func updateBackButtonVisibility() {
        backButton.isHidden = navigationController?.viewControllers.count ?? 0 <= 1
    }
    
    // MARK: - Life Cycle
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.bringSubviewToFront(navigationBarBackground)
        view.bringSubviewToFront(navigationBar)
        updateBackButtonVisibility()
    }
    
    // MARK: - Status Bar
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }
    

}
